package com.hsj.busmanager.controller;

import com.hsj.busmanager.api.*;
import com.hsj.busmanager.service.*;
import com.hsj.busmanager.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/driver")
public class DriverController {

    @Autowired
    private UserService userService;

    @Autowired
    private NewsService newsService;

    @Autowired
    private MountSerivce mountSerivce;

    @Autowired
    private RentSerivce rentSerivce;

    @Autowired
    private OtherCostService otherCostService;

    @RequestMapping(value = "index")
    public String index(){
        return "/driver_index";
    }

    @RequestMapping(value = "info")
    public ModelAndView info(HttpServletRequest request,ModelAndView model){

        UserBean user = (UserBean)request.getSession().getAttribute("user");

        UserBean userBean = userService.getUserById(user.getId());

        if(user.getDriverTime()!=null){
            userBean.setShowDriverTime(CommonUtil.getDateByFormat(user.getDriverTime(),"yyyy-MM-dd"));
        }
        model.addObject("user",userBean);
        model.setViewName("/driver/info");
        return  model;
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public R update(CheckUserBean user){

        //设置为未审核
        user.setIsCheck("0");

        try {
            if(user.getShowDriverTime()!=null){
                user.setDriverTime(CommonUtil.parseDate(user.getShowDriverTime(),"yyyy-MM-dd"));
            }

            userService.saveCheckUsr(user);
            //向管理员发送邮件
            newsService.send("个人信息审核通知","有用户正在修改个人信息，请尽快审核！",user.getId(),6);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("管理员正在审核，请勿重新提交信息！");
        }

        return R.ok("修改成功,请等待管理员审核！");


    }


    @RequestMapping(value = "news")
    public ModelAndView news(ModelAndView model,HttpServletRequest request){

        model.setViewName("/driver/news");
        return model;

    }


    @RequestMapping(value = "getNews")
    @ResponseBody
    public TabelData<News> getNews(Page page,HttpServletRequest request){
        UserBean user = (UserBean) request.getSession().getAttribute("user");
        return newsService.getNewsPage(page,user.getId());
    }

    @RequestMapping(value = "newsRead")
    @ResponseBody
    public R newsRead(News news){
        news.setIsRead("1");
        try {
            newsService.update(news);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("更新消息状态失败！");
        }
        return  R.ok("更新成功！") ;
    }


    /**
     * 跳转到租金管理页面
     * @return
     */
    @RequestMapping(value = "rentIndex")
    public String rentCoseIndex(){
        return "/driver/rent/index";
    }

    /**
     * 跳转到其他费用管理页面
     * @return
     */
    @RequestMapping(value = "costIndex")
    public String costIndex(){
        return "/driver/rent/otherCost";
    }

    /**
     * 跳转到补贴管理页面
     * @return
     */
    @RequestMapping(value = "butieIndex")
    public String butieIndex(){
        return "/driver/butie/index";
    }


    @RequestMapping(value = "getRentData")
    @ResponseBody
    public TabelData<Rent> getRentCostData(Page page, Rent rent, HttpSession session) {

        UserBean user = (UserBean)session.getAttribute("user");

        return  mountSerivce.getrentCostData(page,rent,user.getId());

    }

    @RequestMapping(value = "getOtherCostData")
    @ResponseBody
    public TabelData<Cost> getOtherCostData(Page page, Cost cost,HttpServletRequest request) {
        UserBean user = (UserBean) request.getSession().getAttribute("user");
        return  mountSerivce.getOtherCostData(page,cost,user.getId());

    }

    @RequestMapping(value = "getButieData")
    @ResponseBody
    public TabelData<Butie> getButieData(Page page, Butie butie) {
        return  mountSerivce.getButieData(page,butie);

    }

    /**
     * 跳转到支付页面
     * @param type 支付类型 1：租金 2：其它费用
     * @param id
     * @return
     */
    @RequestMapping(value = "toPay")
    public ModelAndView toPay(String type,Integer id,ModelAndView model){

        model.addObject("type",type);
        model.addObject("id",id);
        model.setViewName("/driver/pay");
        return model;
    }

    @RequestMapping(value = "pay")
    @ResponseBody
    public R pay(String type,Integer id){

        try {
            if(PayConst.RENT.equals(type)){
                //租金
                rentSerivce.updateIsPay(id);
            }else{
                //其他费用
                otherCostService.updateIsPay(id);
            }
        }catch ( Exception e){
            e.printStackTrace();
            return R.error("支付失败");
        }
        return R.ok("支付成功");

    }
}
